# ip生成及存入文件
def ip_create():
    import random
    import json
    global ipshow
    ipshow = 0

    with open("ips.txt", "w+") as fp:
        for i in range(1000):
            str1 = random.randint(1, 256)
            ip = f'172.25.254.{str1}'
            # ips = json.dumps(ip)
            fp.write(ip + '\n')
            # print(ip)

    with open("ips.txt", "r+") as fp:
        ipshow = fp.read()

# ip_create()

def sorted_ip(filename, count=10):
    ips_dict = dict()
    with open(filename) as f:
        for ip in f:
            if ip in ips_dict:
                ips_dict[ip] += 1
            else:
                ips_dict[ip] = 1
    # sorted(iterable,key=None,reverse=False)适用于任何可迭代容器，
    # 调用后返回的是已经排好序的iterable,不改变原来的列表
    # [:count]计算数组中的单元数目，或对象中的属性个数
    sorted_ip = sorted(ips_dict.items(),
                       key=lambda x: x[1], reverse=True)[:count]
    # count.count
    return sorted_ip

print("ip出现前十的是",sorted_ip('ips.txt'))

